package com.cskaoyan.service;

import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.MarketUserExample;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketUserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author yi
 * @date 2022/10/13 11:22
 */

@Service
public class AdminUserServerImpl implements AdminUserServer {

    @Autowired
    MarketUserMapper marketUserMapper;

    @Override
    public CommonData<MarketUser> queryUser(String username, String mobile, BasePageInfo basePageInfo) {
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());
        MarketUserExample marketUserExample = new MarketUserExample();

        marketUserExample.setOrderByClause(basePageInfo.getSort());
        // marketUserExample.setOrderByClause(basePageInfo.getSort() + " " + basePageInfo.getOrder());
        // marketUserExample.setDistinct(true);

        MarketUserExample.Criteria criteria = marketUserExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (username != null && !"".equals(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        if (mobile != null && !"".equals(mobile)) {
            criteria.andMobileEqualTo(mobile);
        }

        List<MarketUser> marketUsers = marketUserMapper.selectByExample(marketUserExample);

        PageInfo<MarketUser> marketUserPageInfo = new PageInfo<>(marketUsers);

        return CommonData.data(marketUserPageInfo);
    }

    @Override
    public MarketUser queryUserById(Integer id) {
        MarketUser marketUser = marketUserMapper.selectByPrimaryKey(id);
        return marketUser;
    }
}
